﻿namespace LeetCodeProject._0001_1000._001_100._91_100._098;

public class Solution
{
    public bool IsValidBST(TreeNode root)
    {
        return IsValid(root, null, null);
    }

    private bool IsValid(TreeNode root, int? minValue, int? maxValue)
    {
        if (root == null)
            return true;
        if (minValue.HasValue && root.val <= minValue)
            return false;
        if (maxValue.HasValue && root.val >= maxValue)
            return false;
        return IsValid(root.left, minValue, root.val) && IsValid(root.right, root.val, maxValue);
    }
}